home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / share / 04 / setup.exe / MM9.Cab / F1488_mscommerce.runtime5.asp.9B3B646D_CB56_4EAE_BAB7_3E7E8E41A649 < prev    next >
Text File  |  2000-08-17  |  18KB  |  563 lines

  1. <SCRIPT runat="server" language="JScript">
  2.     
  3. // *****************************************************************************
  4. //
  5. // include/mscommerce.runtime5.asp
  6. //
  7. // Dynamic Link runtime support for Microsoft Commerce Server
  8. //
  9. // COPYRIGHT (c) 1999-2000 Adobe Systems Incorporated. All rights reserved.
  10. //
  11. // These routines are designed to work stand-alone or within existing commerce 
  12. // sites.  The following globals will be used if found:
  13. //
  14. //     Application("SiteName")
  15. //     Application("DefaultConnectionString")
  16. //
  17. //     Application("MSCSSite")
  18. //     Application("MSCSQueryMap")
  19. //     Application("MSCSMessageManager")
  20. //     Application("MSCSShopperManager")
  21. //     Application("MSCSSIDURLKey")
  22. //     Application("MSCSDataFunctions")
  23. //
  24. //     mscsPage
  25. //     mscsShopperID
  26. //     mscsOrderFormStorage
  27. //     mscsOrderForm
  28. //     mscsPipeContext
  29. //
  30.  
  31. if (typeof(mscsPage)                == "undefined") { var mscsPage; }
  32. if (typeof(mscsShopperID)            == "undefined") { var mscsShopperID; }
  33. if (typeof(mscsOrderFormStorage)    == "undefined") { var mscsOrderFormStorage; }
  34. if (typeof(mscsOrderForm)            == "undefined") { var mscsOrderForm; }
  35. if (typeof(mscsPipeContext)            == "undefined") { var mscsPipeContext; }
  36.  
  37. var mscsOrderFormIsNew = false;
  38. var fakeOrderForm;
  39.  
  40. if (typeof(Session("FakeShoppingBasket")) == "undefined") {
  41.     Session("FakeShoppingBasket") = "";
  42. }
  43.  
  44. // -----------------------------------------------------------------------------
  45. // Place a global variable into the application object.
  46.  
  47. function AddToApplication(globalName)
  48. {
  49.     Application.Lock();
  50.     Application(globalName) = eval(globalName);
  51.     Application.Unlock();
  52. }
  53.  
  54. // -----------------------------------------------------------------------------
  55. // Read the site config file into the global MSCSSite dictionary.
  56.  
  57. function ReadSiteDictionary()
  58. {
  59.     var cscPath = Request("APPL_PHYSICAL_PATH") + "/config/site.csc";
  60.  
  61.     var fso = Server.CreateObject("Scripting.FileSystemObject");
  62.     if (fso.FileExists(cscPath)) {
  63.         var FD = Server.CreateObject("Commerce.FileDocument");
  64.         FD.ReadDictionaryFromFile(cscPath, "IISProperties", MSCSSite);
  65.     } else {
  66.         MSCSSite.CloseRedirectURL = "";
  67.         MSCSSite.DefaultConnectionString = ConnectString(Application("DefaultDatabase"));
  68.         MSCSSite.DisableHTTPS = 1;
  69.         MSCSSite.DisplayName = Application("SiteName");
  70.         MSCSSite.NonsecureHostName = Request("SERVER_NAME") + ":" + Request("SERVER_PORT");
  71.         MSCSSite.SecureHostName = "";
  72.         MSCSSite.Status = "Open";
  73.     }
  74. }
  75.  
  76. // -----------------------------------------------------------------------------
  77. // Load some default messages into the global MSCSMessageManager.
  78.  
  79. function LoadMessages()
  80. {
  81.     MSCSMessageManager.AddMessage("pur_out_of_stock", "At least one item is out of stock.");
  82.     MSCSMessageManager.AddMessage("pur_badsku", "Products in your basket were deleted because they don't exist in this store.");
  83.     MSCSMessageManager.AddMessage("pur_badplacedprice", "Prices of products in your basket have been updated.");
  84.     MSCSMessageManager.AddMessage("pur_noitems", "An order must have at least one item.");
  85.     MSCSMessageManager.AddMessage("pur_badshipping", "Unable to complete order. Cannot compute shipping cost.");
  86.     MSCSMessageManager.AddMessage("pur_badtax", "Unable to complete order. Cannot compute tax.");
  87.     MSCSMessageManager.AddMessage("pur_badhandling", "Unable to complete order. Cannot compute handling cost.");
  88.     MSCSMessageManager.AddMessage("pur_badverify", "Changes to the data require your review. Please review and resubmit.");
  89.     MSCSMessageManager.AddMessage("pur_badpayment", "There was a problem authorizing your credit.  Please verify your payment information or use a different card.");
  90.     MSCSMessageManager.AddMessage("pur_badcc", "Bad Credit Card Number.");
  91. }
  92.  
  93. // -----------------------------------------------------------------------------
  94. // Makes sure all the global-scope MSCS objects are defined.
  95.  
  96. function AssertGlobals()
  97. {
  98.     if (typeof(MSCSSite) == "undefined") {
  99.         MSCSSite = Application("MSCSSite");
  100.     }
  101.     if (typeof(MSCSSite) == "undefined") {
  102.         MSCSSite = Server.CreateObject("Commerce.Dictionary");
  103.         ReadSiteDictionary();
  104.     }
  105.     
  106.     if (typeof(MSCSShopperManager) == "undefined") {
  107.         MSCSShopperManager = Application("MSCSShopperManager");
  108.     }
  109.     if (typeof(MSCSShopperManager) == "undefined") {
  110.         MSCSShopperManager = Server.CreateObject("Commerce.StandardSManager");
  111.         MSCSShopperManager.InitManager(Application("SiteName"), "url");
  112.         AddToApplication("MSCSShopperManager");
  113.     }
  114.  
  115.     if (typeof(MSCSSIDURLKey) == "undefined") {
  116.         MSCSSIDURLKey = Application("MSCSSIDURLKey");
  117.     }
  118.     if (typeof(MSCSSIDURLKey) == "undefined") {
  119.         MSCSSIDURLKey = "mscssid";
  120.         AddToApplication("MSCSSIDURLKey");
  121.     }
  122.     
  123.     if (typeof(MSCSQueryMap) == "undefined") {
  124.         MSCSQueryMap = Application("MSCSQueryMap");
  125.     }
  126.     if (typeof(MSCSQueryMap) == "undefined") {
  127.         MSCSQueryMap = Server.CreateObject("Commerce.Dictionary");
  128.         MSCSQueryMap.productpl = Server.CreateObject("Commerce.Dictionary");
  129.         MSCSQueryMap.productpl.SQLCommand = "SELECT sku, name, list_price FROM Products WHERE sku = ?";
  130.         AddToApplication("MSCSQueryMap");
  131.     }
  132.     
  133.     if (typeof(MSCSMessageManager) == "undefined") {
  134.         MSCSMessageManager = Application("MSCSMessageManager");
  135.     }
  136.     if (typeof(MSCSMessageManager) == "undefined") {
  137.         MSCSMessageManager = Server.CreateObject("Commerce.MessageManager");
  138.         MSCSMessageManager.AddLanguage("usa", 0x409);
  139.         MSCSMessageManager.defaultLanguage = "usa";
  140.         LoadMessages();
  141.         AddToApplication("MSCSMessageManager");
  142.     }
  143.     
  144.     if (typeof(MSCSDataFunctions) == "undefined") {
  145.         MSCSDataFunctions = Application("MSCSDataFunctions");
  146.     }
  147.     if (typeof(MSCSDataFunctions) == "undefined") {
  148.         MSCSDataFunctions = Server.CreateObject("Commerce.DataFunctions");
  149.         MSCSDataFunctions.Locale = 0x409;
  150.         AddToApplication("MSCSDataFunctions");
  151.     }
  152. }
  153.  
  154. // -----------------------------------------------------------------------------
  155. // Add any necessary shopper arguments to a dynamic URL.
  156.  
  157. function mscs_URLArgs()
  158. {
  159.     return queryStringToArray(mscsPage.URLShopperArgs());
  160. }
  161.  
  162. // -----------------------------------------------------------------------------
  163. // Makes sure the page- and session-scope MSCS objects are defined.
  164.  
  165. function AssertPage()
  166. {
  167.     if (typeof(mscsPage) == "undefined") {
  168.         mscsPage = Server.CreateObject("Commerce.Page");
  169.     }
  170.     if (typeof(mscsShopperID) == "undefined") {
  171.         mscsShopperID = mscsPage.GetShopperID();
  172.     }
  173.     if (!mscsShopperID) {
  174.         mscsShopperID = MSCSShopperManager.CreateShopperID();
  175.         mscsPage.PutShopperID(mscsShopperID);
  176.     }
  177. }
  178.  
  179.  
  180. // -----------------------------------------------------------------------------
  181. // Makes sure that an MSCS orderForm exists.
  182.  
  183. function AssertOrderForm()
  184. {
  185.     if (typeof(mscsOrderFormStorage) == "undefined") {
  186.         mscsOrderFormStorage = Server.CreateObject("Commerce.DBStorage");
  187.         mscsOrderFormStorage.InitStorage(
  188.             MSCSSite.DefaultConnectionString,
  189.             "ShoppingBaskets",        // table that holds data
  190.             "shopper_id",            // table key
  191.             "Commerce.OrderForm",    // object that maps onto stored data
  192.             "marshalled_order",        // field containing opaque data
  193.             "date_changed"            // field containing date of last change
  194.         );
  195.     }
  196.     
  197.     if (typeof(mscsOrderForm) == "undefined") {
  198.         mscsOrderForm = mscsOrderFormStorage.GetData(null, mscsShopperID);
  199.         if (!mscsOrderForm) {
  200.             mscsOrderForm = Server.CreateObject("Commerce.OrderForm");
  201.             mscsOrderForm.shopper_id = mscsShopperID;
  202.             mscsOrderFormIsNew = true;
  203.         }
  204.     }
  205. }
  206.  
  207. // -----------------------------------------------------------------------------
  208. // Commit any changes made to the order form to the database.
  209.  
  210. function UpdateOrderForm()
  211. {
  212.     if (mscsOrderFormIsNew) {
  213.         mscsOrderFormStorage.InsertData(null, mscsOrderForm);
  214.         mscsOrderFormIsNew = false;
  215.     } else {
  216.         mscsOrderFormStorage.CommitData(null, mscsOrderForm);
  217.     }
  218. }
  219.  
  220. // -----------------------------------------------------------------------------
  221. // Run the order processing pipeline on an order form.
  222.  
  223. function RunPlan()
  224. {
  225.     if (typeof(mscsPipeContext) == "undefined") {
  226.         mscsPipeContext = Server.CreateObject("Commerce.Dictionary");
  227.         mscsPipeContext.MessageManager = MSCSMessageManager;
  228.         mscsPipeContext.DataFunctions = MSCSDataFunctions;
  229.         mscsPipeContext.QueryMap = MSCSQueryMap;
  230.         mscsPipeContext.ConnectionStringMap = MSCSSite.ConnectionStringMap;
  231.         mscsPipeContext.SiteName = MSCSSite.DisplayName;
  232.         mscsPipeContext.DefaultConnectionString = MSCSSite.DefaultConnectionString;
  233.         mscsPipeContext.Language = "usa";
  234.     }
  235.  
  236.     var pipeline = Server.CreateObject("Commerce.MtsPipeline");
  237.     
  238.     // Comment in the next line for debugging
  239.     // pipeline.SetLogFile(Request("APPL_PHYSICAL_PATH") + "/config/plan.pcf.log");
  240.     pipeline.LoadPipe(Request("APPL_PHYSICAL_PATH") + "/config/plan.pcf");
  241.     pipeline.Execute(1, mscsOrderForm, mscsPipeContext, 0);
  242.     UpdateOrderForm();
  243. }
  244.  
  245.  
  246. // -----------------------------------------------------------------------------
  247. // Returns the current OrderForm wrapped as a Dynamic Link content source.
  248.  
  249. function GetOrderForm(db)
  250. {
  251.     if (UseFakeCommerce()) {
  252.         fakeOrderForm = new CSWFakeOrderForm(db, "Products", "SKU", "list_price");
  253.         return fakeOrderForm;
  254.     }
  255.     
  256.     AssertOrderForm();
  257.     RunPlan();
  258.     return new CSWOrderForm(mscsOrderForm);
  259. }
  260.  
  261.  
  262. // *****************************************************************************
  263. // CONTENT SOURCE WRAPPERS: ORDERFORM and ORDERFORM ITEMS
  264. //
  265. // Items are accessed through the wrappers as follows:
  266. //
  267. //      set orderForm = WrapOrderForm(mscsOrderForm)
  268. //      orderForm.Value("ship_to_name")
  269. //      orderForm.items.MoveFirst
  270. //      orderForm.items.Value("SKU")
  271.  
  272. // -----------------------------------------------------------------------------
  273. // Content source wrapper for OrderForm.
  274.  
  275. function WrapOrderForm(mscsOrderForm)
  276. {
  277.     return new CSWOrderForm(mscsOrderForm);
  278. }
  279.  
  280. function CSWOrderForm(mscsOrderForm)
  281. {
  282.     // Method table:
  283.     this.Move = CSW_NOP;
  284.     this.MoveFirst = CSW_NOP;
  285.     this.MoveNext = CSW_NOP;
  286.     this.Value = CSWOrderForm_Value;
  287.     this.Set = CSWOrderForm_Set;
  288.     this.UpdateBatch = UpdateOrderForm;
  289.  
  290.     this.Data = mscsOrderForm;
  291.     this.items = new CSWDictionaryList(mscsOrderForm.items);
  292.     this.items.Key = CSWOrderFormItems_Key;
  293.     this.items.UpdateBatch = UpdateOrderForm;
  294.     this._Basket_Errors = new CSWVariantList(mscsOrderForm._Basket_Errors);
  295.     this._Basket_Errors.UpdateBatch = UpdateOrderForm;
  296.     this._Purchase_Errors = new CSWVariantList(mscsOrderForm._Purchase_Errors);
  297.     this._Purchase_Errors.UpdateBatch = UpdateOrderForm;
  298.  
  299.     this.RecordCount = 1;
  300.     this.AbsolutePosition = 1;
  301.     this.EOF = true;
  302. }
  303.  
  304. // -----------------------------------------------------------------------------
  305. // Data access:
  306.  
  307. function CSWOrderForm_Value(fieldName)
  308. {
  309.     return this.Data.Value( fieldName );
  310. }
  311.  
  312. function CSWOrderForm_Set(fieldName, value)
  313. {
  314.     this.Data.Value(fieldName) = value;
  315. }
  316.  
  317. // Most of the functionality of the orderForm's item list is provided by the
  318. // DictionaryList CSW.    We add these two methods to automate forms processing
  319. // using the SKU as the key.
  320.  
  321. function CSWOrderFormItems_Key()
  322. {
  323.     return "SKU=" + this.Value("SKU");
  324. }
  325.  
  326.  
  327. // *****************************************************************************
  328. // CONTENT SOURCE WRAPPERS: SIMPLELIST AND DICTIONARY
  329.  
  330. // -----------------------------------------------------------------------------
  331. // Content source wrapper for a Commerce.SimpleList of Commerce.Dictionary's.
  332. // We don't wrap SimpleLists directly 'cause then we'd have to wrap each field
  333. // in the dictionary.
  334.  
  335. function WrapDictionaryList(mscsList)
  336. {
  337.     return new CSWDictionaryList(mscsList);
  338. }
  339.  
  340. function CSWDictionaryList(mscsList)
  341. {
  342.     // Method table:
  343.     this.Move = CSW_Move;
  344.     this.MoveFirst = CSW_MoveFirst;
  345.     this.MoveNext = CSW_MoveNext;
  346.     this.Value = CSWDictionaryList_Value;
  347.     this.Set = CSWDictionaryList_Set;
  348.     this.UpdateBatch = CSW_NOP;
  349.  
  350.     this.Data = mscsList;
  351.     this.RecordCount = this.Data.Count;
  352.     this.MoveFirst();
  353. }
  354.  
  355. // -----------------------------------------------------------------------------
  356. // Data access:
  357.  
  358. function CSWDictionaryList_Value(fieldName)
  359. {
  360.     return this.Data( this.AbsolutePosition - 1 ).Value( fieldName );
  361. }
  362.  
  363. function CSWDictionaryList_Set(fieldName, value)
  364. {
  365.     this.Data(this.AbsolutePosition-1).Value(fieldName) = value;
  366. }
  367.  
  368.  
  369. // -----------------------------------------------------------------------------
  370. // Content source wrapper for a Commerce.SimpleList of Variant's.
  371.  
  372. function WrapVariantList(mscsList)
  373. {
  374.     return new CSWVariantList(mscsList);
  375. }
  376.  
  377. function CSWVariantList(mscsList)
  378. {
  379.     // Method table:
  380.     this.Move = CSW_Move;
  381.     this.MoveFirst = CSW_MoveFirst;
  382.     this.MoveNext = CSW_MoveNext;
  383.     this.Value = CSWVariantList_Value;
  384.     this.Set = CSWVariantList_Set;
  385.     this.UpdateBatch = CSW_NOP;
  386.  
  387.     this.Data = mscsList;
  388.     this.RecordCount = this.Data.Count;
  389.     this.MoveFirst();
  390. }
  391.  
  392. // -----------------------------------------------------------------------------
  393. // Data access:
  394.  
  395. function CSWVariantList_Value()
  396. {
  397.     return this.Data( this.AbsolutePosition - 1 );
  398. }
  399.  
  400. function CSWVariantList_Set(value)
  401. {
  402.     this.Data(this.AbsolutePosition-1) = value;
  403. }
  404.  
  405. // *****************************************************************************
  406. // CONTENT SOURCE WRAPPERS: FAKE ECOMMERCE
  407. //
  408. // Simulate a commerce server.    Shopping basket information is stored in
  409. // the session object.    Prices and other item information are pulled
  410. // from a database.
  411.  
  412. function UseFakeCommerce()
  413. {
  414.     return typeof(Application("UseMSCommerceServer")) == "undefined"
  415.         || Application("UseMSCommerceServer") == false;
  416. }
  417.  
  418. function CSWFakeOrderForm(db, table, sku, price)
  419. {
  420.     this.Init = CSWFakeOrderForm_Init;
  421.     this.GetProductInfo = CSWFakeOrderForm_GetProductInfo;
  422.     this.Init(db, table, sku, price);
  423.     
  424.     // wrapper API methods
  425.     this.Move = CSW_NOP;
  426.     this.MoveFirst = CSW_NOP;
  427.     this.MoveNext = CSW_NOP;
  428.     this.Value = CSWFakeOrderForm_Value;
  429.     this.Set = CSW_NOP;
  430.     this.UpdateBatch = CSWFakeOrderForm_Save;
  431.     this.RecordCount = 1;
  432.     this.AbsolutePosition = 1;
  433.     this.EOF = true;
  434.  
  435.     // wrapper API methods
  436.     this.items.Move = CSW_Move;
  437.     this.items.MoveFirst = CSW_MoveFirst;
  438.     this.items.MoveNext = CSW_MoveNext;
  439.     this.items.Value = CSWFakeItemList_Value;
  440.     this.items.Set = CSW_NOP;
  441.     this.items.UpdateBatch = CSWFakeItemList_Save;
  442.     this.items.Key = CSWOrderFormItems_Key;
  443.     this.items.RecordCount = this.items.data.length;
  444.     this.items.MoveFirst();
  445. }
  446.  
  447. // -----------------------------------------------------------------------------
  448. // Persistence for fake shopping baskets.  Basket information is stored in
  449. // the session data as a string of the form "sku1,qty1,...,skuN,qtyN".    In the
  450. // running object it is stored as a list of lists.
  451.  
  452. function CSWFakeOrderForm_Init(db, table, sku, price)
  453. {
  454.     var storedData = Session("FakeShoppingBasket").split(",");
  455.     var itemList = new Array();
  456.     for (var i = 0; i < Math.floor(storedData.length / 2); i++) {
  457.         itemList[i] = new Object();
  458.         itemList[i].SKU = storedData[2*i];
  459.         itemList[i].Quantity = storedData[2*i + 1];
  460.     }
  461.     this.items = new Object();
  462.     this.items.data = itemList;
  463.     this.GetProductInfo(db, table, sku, price);
  464.     this._total_total = 0;
  465.     for (var i = 0; i < this.items.data.length; i++) {
  466.         this.items.data[i]._iadjust_currentprice = this.items.data[i]._product_list_price;
  467.         this.items.data[i]._oadjust_adjustedprice = this.items.data[i].Quantity * this.items.data[i]._iadjust_currentprice;
  468.         this._total_total += this.items.data[i]._oadjust_adjustedprice;
  469.     }
  470. }
  471.  
  472. function CSWFakeOrderForm_Save()
  473. {
  474.     this.items.UpdateBatch();
  475. }
  476.  
  477. function CSWFakeItemList_Save()
  478. {
  479.     var itemList = this.data;
  480.     var storedData = new Array();
  481.     var j = 0;
  482.     for (var i = 0; i < itemList.length; i++) {
  483.         if (!itemList[i].deleted) {
  484.             storedData[2*j] = itemList[i].SKU;
  485.             storedData[2*j + 1] = itemList[i].Quantity;
  486.             j++;
  487.         }
  488.     }
  489.     Session("FakeShoppingBasket") = storedData.join(",");
  490. }
  491.  
  492. // -----------------------------------------------------------------------------
  493. // Get product information from the database.
  494.  
  495. function CSWFakeOrderForm_GetProductInfo(db, table, sku, price)
  496. {
  497.     if ((this.items.data.length == 0) ||
  498.         (String(db).length == 0) ||
  499.         (String(table).length == 0) ||
  500.         (String(sku).length == 0) ||
  501.         (String(price).length == 0))
  502.     {
  503.         return;
  504.     }
  505.          
  506.     var recordSet = new ActiveXObject("ADODB.RecordSet");
  507.     recordSet.Open("select * from " + table, ConnectString(db));
  508.     for (var i = 0; i < this.items.data.length; i++) {
  509.         recordSet.MoveFirst();
  510.         while (!recordSet.EOF) {
  511.             if (recordSet(sku) != this.items.data[i].SKU) {
  512.                 recordSet.MoveNext();
  513.                 continue;
  514.             }
  515.             for (var j = 0; j < recordSet.Fields.Count; j++) {
  516.                 var name = recordSet.Fields(j).Name;
  517.                 if (name == price)
  518.                     name = "list_price";
  519.                 this.items.data[i]["_product_" + name] = recordSet.Fields(j).Value;
  520.             }
  521.             break;
  522.         }
  523.     }
  524. }
  525.  
  526. // -----------------------------------------------------------------------------
  527. // Retrieve values:
  528.  
  529. function CSWFakeOrderForm_Value(fieldName)
  530. {
  531.     return this[fieldName];
  532. }
  533.  
  534. function CSWFakeItemList_Value(fieldName)
  535. {
  536.     return this.data[this.AbsolutePosition - 1][fieldName];
  537. }
  538.  
  539.  
  540. // *****************************************************************************
  541. // INITIALIZATION
  542.  
  543. function initMSCS()
  544. {
  545.     if (UseFakeCommerce()) {
  546.         return;
  547.     }
  548.     
  549.     AssertGlobals();
  550.     AssertPage();
  551.  
  552.     RegisterURLArgsFunction(mscs_URLArgs);
  553. }
  554.  
  555. // Note: since we don't know the source page language, the following must
  556. // be language neutral:
  557.  
  558. </SCRIPT>
  559.  
  560. <%
  561.     initMSCS()
  562. %>
  563.